from header_game_menus import *

from module_constants import *

####################################################################################################################
#  (menu-id, menu-flags, menu_text, mesh-name, [<operations>], [<options>]),
#
#   Each game menu is a tuple that contains the following fields:
#  
#  1) Game-menu id (string): used for referencing game-menus in other files.
#     The prefix menu_ is automatically added before each game-menu-id
#
#  2) Game-menu flags (int). See header_game_menus.py for a list of available flags
#  3) Game-menu text (string).
#  4) mesh-name (string). Not currently used. Must be the string "none"
#  5) Operations block (list). A list of operations. See header_operations.py for reference.
#     The operations block is executed when the game menu is activated.
#  6) List of Menu options (List).
#     Each menu-option record is a tuple containing the following fields:
#   6.1) Menu-option-id (string) used for referencing game-menus in other files.
#        The prefix mno_ is automatically added before each menu-option.
#   6.2) Conditions block (list). This must be a valid operation block. See header_operations.py for reference. 
#        The conditions are executed for each menu option to decide whether the option will be shown to the player or not.
#   6.3) Menu-option text (string).
#   6.4) Consequences block (list). This must be a valid operation block. See header_operations.py for reference. 
#        The consequences are executed for the menu option that has been selected by the player.
#
#
# Note: The first Menu is the initial character creation menu.
####################################################################################################################

game_menus = [
  (
    "start_game_1",0,
    "Welcome, adventurer, to Mount&Blade. Before you start the game, you must create a character. To begin, select your character's gender.",
    "none",
    [],
    [
      ("start_male",[],"Male",
       [
           (troop_set_type,"trp_player",0),
           (assign,"$character_gender",0),
           (troop_raise_attribute, "trp_player",ca_strength,1),
           (troop_raise_attribute, "trp_player",ca_charisma,1),
           (jump_to_menu,"mnu_start_game_2")
        ]
       ),
      ("start_female",[],"Female",
       [
           (troop_set_type,"trp_player",1),
           (assign,"$character_gender",1),
           (troop_raise_attribute, "trp_player",ca_agility,1),
           (troop_raise_attribute, "trp_player",ca_intelligence,1),
           (jump_to_menu,"mnu_start_game_2")
        ]
       ),
    ]
  ),
  (
    "tutorial_question",0,
    "Mount&Blade includes a tutorial that teaches the basics of movement and combat. Do you want to enter the tutorial now?",
    "none",
    [
    ],
    [
      ("tutorial_question_yes",[]," Yes. I want to improve my weapon skills in the tutorial.",[
          (jump_to_menu, "mnu_training_ground"),
                                ]),
      ("tutorial_question_no",[],"No. Let me go straight to the game.",[(change_screen_return)]),
    ]
  ),  
  (
    "start_game_2",0,
    "Before taking up a life of adventuring, you used to be...",
    "none",
    [],
    [
      ("start_squire",[(eq,"$character_gender",0)],"a squire",[
        (assign,"$character_class",0),
        (troop_raise_attribute, "trp_player",ca_strength,5),
        (troop_raise_attribute, "trp_player",ca_agility,2),
        (troop_raise_attribute, "trp_player",ca_charisma,1),
        (troop_raise_skill, "trp_player",skl_ironflesh,2),
        (troop_raise_skill, "trp_player",skl_weapon_master,2),
        (troop_raise_skill, "trp_player",skl_power_strike,3),
        (troop_raise_skill, "trp_player",skl_riding,1),
        (troop_raise_skill, "trp_player",skl_tactics,1),
        (troop_raise_skill, "trp_player",skl_shield,1),
        (troop_raise_skill, "trp_player",skl_leadership,1),
        (troop_raise_skill, "trp_player",skl_prisoner_management,1),
        (troop_raise_proficiency, "trp_player",0,40),
        (troop_raise_proficiency, "trp_player",1,40),
        (troop_raise_proficiency, "trp_player",2,30),
        (troop_raise_proficiency, "trp_player",5,20),
        (troop_add_item, "trp_player","itm_leather_jerkin",imod_ragged),
        (troop_add_item, "trp_player","itm_arming_sword",imod_rusty),
#        (troop_add_item, "trp_player","itm_short_sword",imod_rusty),
        (troop_add_item, "trp_player","itm_hunting_crossbow",0),
        (troop_add_item, "trp_player","itm_leather_boots",0),
        (troop_add_item, "trp_player","itm_leather_gloves",imod_tattered),
        (troop_add_item, "trp_player","itm_bolts",0),
        (troop_add_item, "trp_player","itm_smoked_fish",0),
        (troop_add_item, "trp_player","itm_saddle_horse",imod_swaybacked),
        [troop_add_gold, "trp_player", 35],
        (change_screen_return,0),
        ]),
      ("start_lady",[(eq,"$character_gender",1)],"a court lady",[
        (assign,"$character_class",1),
        (troop_raise_attribute, "trp_player",ca_strength,1),
        (troop_raise_attribute, "trp_player",ca_agility,1),
        (troop_raise_attribute, "trp_player",ca_intelligence,3),
        (troop_raise_attribute, "trp_player",ca_charisma,3),
        (troop_raise_skill, "trp_player",skl_weapon_master,1),
        (troop_raise_skill, "trp_player",skl_riding,2),
        (troop_raise_skill, "trp_player",skl_wound_treatment,2),
        (troop_raise_skill, "trp_player",skl_first_aid,2),
        (troop_raise_skill, "trp_player",skl_tactics,1),
        (troop_raise_skill, "trp_player",skl_leadership,2),
        (troop_raise_skill, "trp_player",skl_trade,2),
        (troop_raise_proficiency, "trp_player",0,15),
        (troop_raise_proficiency, "trp_player",4,10),
        (troop_raise_proficiency, "trp_player",5,10),
        (troop_add_item, "trp_player","itm_woolen_dress",imod_sturdy),
        (troop_add_item, "trp_player","itm_dagger",imod_watered_steel),
        (troop_add_item, "trp_player","itm_hunting_crossbow",0),
        (troop_add_item, "trp_player","itm_bolts",0),
        (troop_add_item, "trp_player","itm_leather_boots",0),
        (troop_add_item, "trp_player","itm_smoked_fish",0),
        (troop_add_item, "trp_player","itm_courser",imod_spirited),
        [troop_add_gold, "trp_player", 35],
        (change_screen_return,0),
        ]),
      ("start_hunter",[],"a hunter",[
        (assign,"$character_class",2),
        (troop_raise_attribute, "trp_player",ca_strength,3),
        (troop_raise_attribute, "trp_player",ca_agility,4),
        (troop_raise_attribute, "trp_player",ca_intelligence,1),
        (troop_raise_skill, "trp_player",skl_athletics,3),
        (troop_raise_skill, "trp_player",skl_power_draw,3),
        (troop_raise_skill, "trp_player",skl_pathfinding,2),
        (troop_raise_skill, "trp_player",skl_tracking,2),
        (troop_raise_skill, "trp_player",skl_spotting,2),
        (troop_raise_proficiency, "trp_player",3,70),
        (troop_add_item, "trp_player","itm_hunting_bow",0),
        (troop_add_item, "trp_player","itm_arrows",0),
        (troop_add_item, "trp_player","itm_hatchet",0),
        (troop_add_item, "trp_player","itm_rawhide_coat",0),
        (troop_add_item, "trp_player","itm_hide_boots",0),
        (troop_add_item, "trp_player","itm_dried_meat",0),
        (troop_add_item, "trp_player","itm_sumpter_horse",0),
        (troop_add_item, "trp_player","itm_furs",0),
        [troop_add_gold, "trp_player", 35],
        (change_screen_return,0),
        ]),
      ("start_priest",[(eq,"$character_gender",0)],"a novice priest",[
        (assign,"$character_class",3),
        (troop_raise_attribute, "trp_player",ca_strength,1),
        (troop_raise_attribute, "trp_player",ca_agility,1),
        (troop_raise_attribute, "trp_player",ca_intelligence,5),
        (troop_raise_attribute, "trp_player",ca_charisma,1),
        (troop_raise_skill, "trp_player",skl_ironflesh,2),
        (troop_raise_skill, "trp_player",skl_wound_treatment,3),
        (troop_raise_skill, "trp_player",skl_first_aid,3),
        (troop_raise_skill, "trp_player",skl_leadership,2),
        (troop_raise_skill, "trp_player",skl_surgery,2),
        (troop_raise_proficiency, "trp_player",2,15),
        (troop_add_item, "trp_player","itm_robe",0),
        (troop_add_item, "trp_player","itm_staff",0),
        (troop_add_item, "trp_player","itm_grain",0),
        [troop_add_gold, "trp_player", 35],
        (change_screen_return,0),
        ]),
      ("start_nun",[(eq,"$character_gender",1)],"a nun",[
        (assign,"$character_class",3),
        (troop_raise_attribute, "trp_player",ca_intelligence,6),
        (troop_raise_attribute, "trp_player",ca_charisma,1),
        (troop_raise_attribute, "trp_player",ca_agility,1),
        (troop_raise_skill, "trp_player",skl_athletics,1),
        (troop_raise_skill, "trp_player",skl_wound_treatment,4),
        (troop_raise_skill, "trp_player",skl_first_aid,4),
        (troop_raise_skill, "trp_player",skl_leadership,1),
        (troop_raise_skill, "trp_player",skl_surgery,2),
        (troop_add_item, "trp_player","itm_robe",0),
        (troop_add_item, "trp_player","itm_staff",0),
        (troop_add_item, "trp_player","itm_grain",0),
        [troop_add_gold, "trp_player", 35],
        (change_screen_return,0),
        ]),
      ("start_merchant",[],"a merchant",[
        (assign,"$character_class",3),
        (troop_raise_attribute, "trp_player",ca_strength,1),
        (troop_raise_attribute, "trp_player",ca_agility,1),
        (troop_raise_attribute, "trp_player",ca_intelligence,3),
        (troop_raise_attribute, "trp_player",ca_charisma,3),
        (troop_raise_skill, "trp_player",skl_riding,2),
        (troop_raise_skill, "trp_player",skl_inventory_management,3),
        (troop_raise_skill, "trp_player",skl_pathfinding,1),
        (troop_raise_skill, "trp_player",skl_trade,3),
        (troop_raise_skill, "trp_player",skl_leadership,3),
        (troop_raise_proficiency, "trp_player",0,15),
        (troop_raise_proficiency, "trp_player",3,10),
        (troop_add_item, "trp_player","itm_leather_jacket",0),
        (troop_add_item, "trp_player","itm_leather_boots",0),
        (troop_add_item, "trp_player","itm_dagger",0),
        (troop_add_item, "trp_player","itm_winged_mace",0),
        (troop_add_item, "trp_player","itm_hunting_bow",0),
        (troop_add_item, "trp_player","itm_arrows",0),
        (troop_add_item, "trp_player","itm_smoked_fish",0),
        (troop_add_item, "trp_player","itm_saddle_horse",0),
        (troop_add_item, "trp_player","itm_sumpter_horse",0),
        (troop_add_item, "trp_player","itm_salt",0),
        (troop_add_item, "trp_player","itm_salt",0),
        (troop_add_item, "trp_player","itm_salt",0),
        (troop_add_item, "trp_player","itm_salt",0),
        (troop_add_item, "trp_player","itm_pottery",0),
        (troop_add_item, "trp_player","itm_pottery",0),
        (troop_add_item, "trp_player","itm_pottery",0),
        [troop_add_gold, "trp_player", 65],
        (change_screen_return,0),
        ]),
    ]
  ),
  (
    "simple_encounter",0,
    "You have encountered {s1}. You have {reg10} troops fit for battle against their {reg11}.",
    "none",
    [
        (str_store_party_name, 1,"$g_encountered_party"),
        (party_collect_attachments_to_party, "$g_encountered_party", "p_collective_enemy"),
        (call_script, "script_party_count_fit_for_battle", "p_main_party"),
        (assign, reg(10), reg(0)),
        (call_script, "script_party_count_fit_for_battle", "p_collective_enemy"),
        (assign, reg(11), reg(0)),
        (try_begin),
          (eq, "$new_encounter", 1),
          (assign, "$new_encounter", 0),
          (assign, "$capture_screen_shown", 0),
          (assign, "$loot_screen_shown", 0),
          (assign, "$g_battle_result", 0),
          (assign, "$cant_leave_encounter", 0),
          (assign, "$cant_talk_to_enemy_party", 0),
          (assign, "$last_defeated_hero", 0),
          (assign, "$last_freed_hero", 0),
          (call_script, "script_party_copy", "p_main_party_backup", "p_main_party"),
          (call_script, "script_party_copy", "p_encountered_party_backup", "p_collective_enemy"),
          (store_faction_of_party, reg(8), "$g_encountered_party"),
          (store_relation, reg(12), reg(8), "p_main_party"),
          (assign, "$encountered_party_hostile", 0),
          (assign, "$encountered_party_friendly", 0),
          (try_begin),
            (gt, reg(12), 0),
            (assign, "$encountered_party_friendly", 1),
          (try_end),
          (try_begin),
            (lt, reg(12), 0),
            (assign, "$encountered_party_hostile", 1),
            (try_begin),
              (encountered_party_is_attacker),
              (assign, "$cant_leave_encounter", 1),
            (try_end),
          (try_end),
          (assign, "$talk_context", tc_party_encounter),
          (assign, "$cant_talk_to_enemy_party", 1),
          (call_script, "script_setup_party_meeting", "$g_encountered_party"),
        (else_try), #second or more turn
          (try_begin),
            # We can leave battle only after some troops have been killed. 
            (eq, "$cant_leave_encounter", 1),
            (call_script, "script_party_count_fit_regulars", "p_main_party_backup"),
            (assign, ":org_total_party_counts", reg0),
            (call_script, "script_party_count_fit_regulars", "p_encountered_party_backup"),
            (val_add, ":org_total_party_counts", reg0),

            (call_script, "script_party_count_fit_regulars", "p_main_party"),
            (assign, ":cur_total_party_counts", reg0),
            (call_script, "script_party_count_fit_regulars", "p_collective_enemy"),
            (val_add, ":cur_total_party_counts", reg0),
          
            (neq, ":cur_total_party_counts", ":org_total_party_counts"),
            (assign, "$cant_leave_encounter", 0),
          (try_end),
          (eq, "$g_leave_encounter",1),
          (change_screen_return),
        (try_end),
        (try_begin),
#          (eq, "$encountered_party_hostile", 1),
          (call_script, "script_party_count_fit_regulars","p_collective_enemy"),
          (assign, reg(3), reg(0)),
          (assign, reg(20),0),
          (try_begin),
            (eq, "$g_battle_result", 1),
            (eq, reg(3), 0), #battle won
            (assign, reg(20),1),
          (try_end),
          (this_or_next|eq, reg(20),1),
          (eq,"$g_enemy_surrenders",1),
          (try_begin),
            # Talk to enemy leaders
            (assign, ":done", 0),
            (party_get_num_companion_stacks, ":num_stacks","p_encountered_party_backup"),
            (try_for_range, ":stack_no", "$last_defeated_hero", ":num_stacks"),
              (party_stack_get_troop_id,   ":stack_troop","p_encountered_party_backup",":stack_no"),
              (troop_is_hero, ":stack_troop"),
              (store_add, "$last_defeated_hero", ":stack_no", 1),
              (assign, "$talk_context", tc_hero_defeated),
              (call_script, "script_setup_troop_meeting",":stack_troop"),
              (assign, ":done", 1),
            (try_end),
            (eq, ":done", 1),
          (else_try),
            # Talk to freed heroes
            (assign, ":done", 0),
            (party_get_num_prisoner_stacks, ":num_prisoner_stacks","p_encountered_party_backup"),
            (try_for_range, ":stack_no", "$last_freed_hero", ":num_prisoner_stacks"),
              (party_prisoner_stack_get_troop_id,   ":stack_troop","p_encountered_party_backup",":stack_no"),
              (troop_is_hero, ":stack_troop"),
              (store_add, "$last_freed_hero", ":stack_no", 1),
              (assign, "$talk_context", tc_hero_freed),
              (call_script, "script_setup_troop_meeting",":stack_troop"),
              (assign, ":done", 1),
            (try_end),
            (eq, ":done", 1),
          (else_try),
            (eq, "$capture_screen_shown", 0),
            (assign, "$capture_screen_shown", 1),
            (party_clear, "p_temp_party"),
            (assign, "$g_move_heroes", 0),
            (call_script, "script_party_prisoners_add_party_companions", "p_temp_party", "p_collective_enemy"),
            (call_script, "script_party_add_party_prisoners", "p_temp_party", "p_collective_enemy"),
            (party_get_num_companions, ":num_rescued_prisoners", "p_temp_party"),
            (party_get_num_prisoners,  ":num_captured_enemies", "p_temp_party"),
            (store_add, ":total_capture_size", ":num_rescued_prisoners", ":num_captured_enemies"),
            (gt, ":total_capture_size", 0),
            (change_screen_exchange_with_party, "p_temp_party"),
          (else_try),
            (eq, "$loot_screen_shown", 0),
            (assign, "$loot_screen_shown", 1),
            (troop_clear_inventory, "trp_temp_troop"),
            (call_script, "script_party_calculate_loot", "p_encountered_party_backup", -1),
            (gt, reg0, 0),
            (change_screen_loot, "trp_temp_troop"),
          (else_try),
            (call_script, "script_party_give_xp_and_gold", "p_encountered_party_backup", -1),
            (party_get_template_id, ":enemy_party_template", "$g_encountered_party"),
#            (remove_party, "$g_encountered_party"),
#            (clear_party_group, "$g_encountered_party"),
            (try_begin),
              (eq, "$g_encountered_party", 0),
              (display_message,"str_error_string"),
            (try_end),
            (call_script, "script_clear_party_group", "$g_encountered_party"),
            (leave_encounter),
            (change_screen_return),
          (try_end),
        (else_try),
#          (eq, "$encountered_party_hostile", 1),
          (call_script, "script_party_count_fit_regulars","p_main_party"),
          (assign, reg(3), reg(0)),
          (assign, reg(20),0),
          (try_begin),
            (eq, "$g_battle_result", -1),
            (eq, reg(3), 0), #battle lost
            (assign, reg(20),1),
          (try_end),
          (this_or_next|eq, reg(20),1),
          (eq,"$g_player_surrenders",1),
          (assign, "$g_move_heroes", 0),
          
          (party_clear, "p_temp_party"),
          (call_script, "script_party_add_party_prisoners", "p_temp_party", "p_main_party"),
          (call_script, "script_party_prisoners_add_party_companions", "p_temp_party", "p_main_party"),
          (distribute_party_among_party_group, "p_temp_party", "$g_encountered_party"),
        
          (call_script, "script_party_remove_all_companions", "p_main_party"),
          (call_script, "script_party_remove_all_prisoners", "p_main_party"),
#          (leave_encounter),
          (try_begin),
            (eq,"$g_player_surrenders",1),
            (jump_to_menu, "mnu_captivity_start_wilderness_surrender"), 
          (else_try),
            (jump_to_menu, "mnu_captivity_start_wilderness_defeat"), 
          (try_end),
#          (change_screen_return),
        (try_end),
    ],
    [
      ("encounter_attack",[
          (eq, "$encountered_party_friendly", 0),
          (store_troop_health,reg(5)),
          (ge,reg(5),5),
          ],
                            "Charge the enemy.",[
                                (assign, "$cant_talk_to_enemy_party", 0),
                                (assign, "$g_battle_result", 0),
                                (assign, "$g_battle_needs_ack", 1),
                                
                                (call_script, "script_calculate_battle_advantage", "p_collective_enemy", -1),
                                (set_battle_advantage, reg0),
                                (set_party_battle_mode),
                                (set_jump_mission,"mt_lead_charge"),
                                (jump_to_scene,"scn_random_scene"),
                                (change_screen_mission),
                                ]),
      ("encounter_order_attack",[
          (eq, "$encountered_party_friendly", 0),
          (call_script, "script_party_count_fit_regulars", "p_main_party"),(ge, reg(0), 2),
          ],
           "Order your troops to attack as you stay back.",[(jump_to_menu,"mnu_order_attack_begin"),
                                                            #(simulate_battle,3)
                                                            ]),
      ("encounter_leave",[
          (eq,"$cant_leave_encounter", 0),
          ],"Leave.",[(leave_encounter),(change_screen_return)]),
      ("encounter_surrender",[
         (eq,"$cant_leave_encounter", 1),
          ],"Surrender.",[(assign,"$g_player_surrenders",1)]),
    ]
  ),
  (
    "order_attack_begin",0,
    "Your troops are ready to attack the enemy...",
    "none",
    [],
    [
      ("order_attack_begin",[],"Order the attack to begin.", [
                                    (assign, "$cant_talk_to_enemy_party", 0),
                                    (jump_to_menu,"mnu_order_attack_2"),
                                    ]),
      ("call_back",[],"Call them back.",[(jump_to_menu,"mnu_simple_encounter")]),
    ]
  ),
  (
    "order_attack_2",0,
    "{s3} Your casualties: {s1}. Enemy casualties: {s2}.",
    "none",
    [
                                    (call_script, "script_party_calculate_regular_strength", "p_main_party"),
                                    (assign, ":player_party_strength", reg0),
                                    (val_div, ":player_party_strength", 5),
                                    (call_script, "script_party_calculate_regular_strength", "p_collective_enemy"),
                                    (assign, ":enemy_party_strength", reg0),
                                    (val_div, ":enemy_party_strength", 5),
                                    
#                                    (call_script,"script_inflict_casualties_to_party", "p_main_party", ":enemy_party_strength"),
                                    (inflict_casualties_to_party_group, "p_main_party", ":enemy_party_strength", "p_temp_casualties"),
                                    (call_script, "script_print_casualties_to_s0", "p_temp_casualties"),
                                    (str_store_string_reg, s1, s0),
                                    
####                                    (call_script,"script_inflict_casualties_to_party", "$g_encountered_party", ":player_party_strength"),
                                    (inflict_casualties_to_party_group, "$g_encountered_party", ":player_party_strength", "p_temp_casualties"),
                                    (call_script, "script_print_casualties_to_s0", "p_temp_casualties"),
                                    (str_store_string_reg, s2, s0),

                                    (party_collect_attachments_to_party, "$g_encountered_party", "p_collective_enemy"),


                                    (assign, "$cant_leave_encounter", 0),

                                    (assign, "$no_soldiers_left", 0),
                                    (try_begin),
                                      (call_script, "script_party_count_fit_regulars","p_main_party"),
                                      (le, reg(0), 0),
                                      (assign, "$no_soldiers_left", 1),
                                      (str_store_string, s3, "str_order_attack_failure"),
                                    (else_try),
                                      (call_script, "script_party_count_fit_regulars","p_collective_enemy"),
                                      (le, reg(0), 0),
                                      (assign, ":continue", 0),
                                      (party_get_num_companion_stacks, ":party_num_stacks", "p_collective_enemy"),
                                      (try_begin),
                                        (eq, ":party_num_stacks", 0),
                                        (assign, ":continue", 1),
                                      (else_try),
                                        (party_stack_get_troop_id, ":party_leader", "p_collective_enemy", 0),
                                        (neg|troop_is_hero, ":party_leader"),
                                        (assign, ":continue", 1),
                                      (try_end),
                                      (eq, ":continue", 1),
                                      (assign, "$g_battle_result", 1),
                                      (assign, "$no_soldiers_left", 1),
                                      (str_store_string, s3, "str_order_attack_success"),
                                    (else_try),
                                      (str_store_string, s3, "str_order_attack_continue"),
                                    (try_end),
    ],
    [
      ("order_attack_continue",[(eq,  "$no_soldiers_left", 0)],"Order your soldiers to continue the attack.",[
          (jump_to_menu,"mnu_order_attack_2"),
          ]),
      ("order_retreat",[(eq,  "$no_soldiers_left", 0)],"Call your soldiers back.",[
          (jump_to_menu,"mnu_simple_encounter"),
          ]),
      ("continue",[(eq,  "$no_soldiers_left", 1)],"Continue...",[
          (jump_to_menu,"mnu_simple_encounter"),
          ]),
    ]
  ),
  
  (
    "pre_join",0,
    "You come accross a battle where {s2} are attacking {s1}. You decide to...",
    "none",
    [
        (str_store_party_name, 1,"$g_encountered_party"),
        (str_store_party_name, 2,"$g_encountered_party_2"),
      ],
    [
      ("pre_join_help_attackers",[
          (store_faction_of_party, ":attacker_faction", "$g_encountered_party_2"),
          (store_relation, ":attacker_relation", ":attacker_faction", "fac_player_faction"),
          (store_faction_of_party, ":defender_faction", "$g_encountered_party"),
          (store_relation, ":defender_relation", ":defender_faction", "fac_player_faction"),
          (ge, ":attacker_relation", -1),
          (le, ":defender_relation", -1),
          ],
          "Go and help the {s2}.",[
              (select_enemy,0),
              (assign,"$g_enemy_party","$g_encountered_party"),
              (assign,"$g_ally_party","$g_encountered_party_2"),
              (jump_to_menu,"mnu_join_battle")]),
      ("pre_join_help_defenders",[
          (store_faction_of_party, ":attacker_faction", "$g_encountered_party_2"),
          (store_relation, ":attacker_relation", ":attacker_faction", "fac_player_faction"),
          (store_faction_of_party, ":defender_faction", "$g_encountered_party"),
          (store_relation, ":defender_relation", ":defender_faction", "fac_player_faction"),
          (ge, ":defender_relation", -1),
          (le, ":attacker_relation", -1),
          ],
          "Rush to the aid of the {s1}.",[
              (select_enemy,1),
              (assign,"$g_enemy_party","$g_encountered_party_2"),
              (assign,"$g_ally_party","$g_encountered_party"),
              (jump_to_menu,"mnu_join_battle")]),
      ("pre_join_leave",[],"Don't get involved.",[(leave_encounter),(change_screen_return)]),
    ]
  ),
  (
    "join_battle",0,
    "You are helping the {s2} against the {s1}. You have {reg10} troops fit for battle against the enemy's {reg11}.",
    "none",
    [
        (str_store_party_name, 1,"$g_enemy_party"),
        (str_store_party_name, 2,"$g_ally_party"),

        (call_script, "script_party_count_fit_for_battle", "p_main_party"),
        (assign, reg(10), reg(0)),
        (party_collect_attachments_to_party, "$g_ally_party", "p_collective_ally"),
        (call_script, "script_party_count_fit_for_battle", "p_collective_ally"),
        (val_add, reg(10), reg(0)),
        (party_collect_attachments_to_party, "$g_enemy_party", "p_collective_enemy"),
        (call_script, "script_party_count_fit_for_battle", "p_collective_enemy"),
        (assign, reg(11), reg(0)),
        (try_begin),
          (eq, "$new_encounter", 1),
          (assign, "$new_encounter", 0),
          (assign, "$capture_screen_shown", 0),
          (assign, "$loot_screen_shown", 0),
          (assign, "$g_battle_result", 0),
          (assign, "$cant_leave_encounter", 0),
          (assign, "$cant_talk_to_enemy_party", 0),
          (assign, "$last_defeated_hero", 0),
          (assign, "$last_freed_hero", 0),
          (call_script, "script_party_copy", "p_main_party_backup", "p_main_party"),
          (call_script, "script_party_copy", "p_encountered_party_backup", "p_collective_enemy"),
          (call_script, "script_party_copy", "p_ally_party_backup", "p_collective_ally"),
          (store_faction_of_party, ":enemy_faction", "$g_enemy_party"),
          (store_relation, reg(12), ":enemy_faction", "p_main_party"),
          (assign, "$enemy_party_hostile", 0),
        (else_try), #second or more turn
          (eq, "$g_leave_encounter",1),
          (change_screen_return),
        (try_end),
        (try_begin),
          (call_script, "script_party_count_fit_regulars","p_collective_enemy"),
          (assign, ":enemy_num_soldiers", reg(0)),
          (assign, ":final_battle_won",0),
          (try_begin),
            (eq, "$g_battle_result", 1),
            (eq, ":enemy_num_soldiers", 0), #battle won
            (assign, ":final_battle_won",1),
          (try_end),
          (this_or_next|eq, ":final_battle_won",1),
          (eq,"$g_enemy_surrenders",1),
          (try_begin),
            # Talk to enemy leaders
            (assign, ":done", 0),
            (party_get_num_companion_stacks, ":num_stacks", "p_encountered_party_backup"),
            (try_for_range, ":stack_no", "$last_defeated_hero", ":num_stacks"),
              (party_stack_get_troop_id,   ":stack_troop","p_encountered_party_backup",":stack_no"),
              (troop_is_hero, ":stack_troop"),
              (store_add, "$last_defeated_hero", ":stack_no", 1),
              (assign, "$talk_context", tc_hero_defeated),
              (call_script, "script_setup_troop_meeting",":stack_troop"),
              (assign, ":done", 1),
            (try_end),
            (eq, ":done", 1),
          (else_try),
            # Talk to freed heroes
            (assign, ":done", 0),
            (party_get_num_prisoner_stacks, ":num_prisoner_stacks","p_encountered_party_backup"),
            (try_for_range, ":stack_no", "$last_freed_hero", ":num_prisoner_stacks"),
              (party_prisoner_stack_get_troop_id,   ":stack_troop","p_encountered_party_backup",":stack_no"),
              (troop_is_hero, ":stack_troop"),
              (store_add, "$last_freed_hero", ":stack_no", 1),
              (assign, "$talk_context", tc_hero_freed),
              (call_script, "script_setup_troop_meeting",":stack_troop"),
              (assign, ":done", 1),
            (try_end),
            (eq, ":done", 1),
          (else_try),
            (eq, "$capture_screen_shown", 0),
            (assign, "$capture_screen_shown", 1),
            (party_clear, "p_temp_party"),
            (assign, "$g_move_heroes", 0),
            (call_script, "script_party_prisoners_add_party_companions", "p_temp_party", "p_collective_enemy"),
            (call_script, "script_party_add_party_prisoners", "p_temp_party", "p_collective_enemy"),
            (call_script, "script_party_calculate_strength", "p_main_party_backup"),
            (assign,":player_party_initial_strength", reg(0)),
            (call_script, "script_party_calculate_strength", "p_ally_party_backup"),
            (assign,":ally_party_initial_strength", reg(0)),

          #TODO: move ally_party_initial_strength/(player_party_initial_strength + ally_party_initial_strength) prisoners to ally party.
             # First we collect the share of prisoners of the ally party and distribute those among the allies.

            (store_add, ":total_initial_strength", ":player_party_initial_strength", ":ally_party_initial_strength"),
            (store_mul, ":ally_share", ":ally_party_initial_strength", 1000),
            (val_div, ":ally_share", ":total_initial_strength"),
            (assign, "$pin_number", ":ally_share"), #we send this as a parameter to the script.
            (party_clear, "p_temp_party_2"),
            (call_script, "script_move_members_with_ratio", "p_temp_party", "p_temp_party_2"),
            (distribute_party_among_party_group, "p_temp_party_2", "$g_ally_party"),

             #next if there's anything left, we'll open up the party exchange screen and offer them to the player.
        
            (party_get_num_companions, ":num_rescued_prisoners", "p_temp_party"),
            (party_get_num_prisoners,  ":num_captured_enemies", "p_temp_party"),
            (store_add, ":total_capture_size", ":num_rescued_prisoners", ":num_captured_enemies"),
            (gt, ":total_capture_size", 0),
            (change_screen_exchange_with_party, "p_temp_party"),
          (else_try),
            (eq, "$loot_screen_shown", 0),
            (assign, "$loot_screen_shown", 1),
            (call_script, "script_party_prisoners_add_party_prisoners", "$g_ally_party", "p_temp_party"), #Add remaining prisoners to ally TODO: FIX it.
            (troop_clear_inventory, "trp_temp_troop"),
            (call_script, "script_party_calculate_loot", "p_encountered_party_backup", "p_ally_party_backup"),
            (gt, reg0, 0),
            (change_screen_loot, "trp_temp_troop"),
          (else_try),
            (end_current_battle),
            (call_script, "script_party_give_xp_and_gold", "p_encountered_party_backup", "p_ally_party_backup"),
#            (clear_party_group, "$g_enemy_party"),
            (try_begin),
              (eq, "$g_enemy_party", 0),
              (display_message,"str_error_string"),
            (try_end),
            (call_script, "script_clear_party_group", "$g_enemy_party"),
            (leave_encounter),
            (change_screen_return),
          (try_end),
        (else_try),
#          (eq, "$encountered_party_hostile", 1),
          (call_script, "script_party_count_fit_regulars","p_main_party"),
          (assign, ":ally_num_soldiers", reg(0)),
          (call_script, "script_party_count_fit_regulars","p_collective_ally"),
          (val_add, ":ally_num_soldiers", reg(0)),
          (assign, ":battle_lost", 0),
          (try_begin),
            (eq, "$g_battle_result", -1),
            (eq, ":ally_num_soldiers", 0), #battle lost
            (assign, ":battle_lost",1),
          (try_end),
          (this_or_next|eq, ":battle_lost",1),
          (eq,"$g_player_surrenders",1),
        # TODO: Split prisoners to all collected parties.
        # NO Need? Let default battle logic do it for us. 
#          (assign, "$g_move_heroes", 0),
#          (call_script, "script_party_add_party_prisoners", "$g_enemy_party", "p_collective_ally"),
#          (call_script, "script_party_prisoners_add_party_companions", "$g_enemy_party", "p_collective_ally"),
        #TODO: Clear all attached allies.
#          (call_script, "script_party_remove_all_companions", "$g_ally_party"),
#          (call_script, "script_party_remove_all_prisoners", "$g_ally_party"),
          (leave_encounter),
          (change_screen_return),
        (try_end),
      ],
    [
      ("join_attack",[
#          (neq, "$encountered_party_hostile", 0),
          (store_troop_health,reg(5),"trp_player"),
          (ge,reg(5),20),
          ],
                            "Charge the enemy.",[
                                (assign, "$cant_talk_to_enemy_party", 0),
                                (assign, "$cant_leave_encounter", 0),
                                (assign, "$g_battle_result", 0),
                                (assign, "$g_battle_needs_ack", 1),

                                (call_script, "script_calculate_battle_advantage", "p_collective_enemy", "p_collective_ally"),
                                (set_battle_advantage, reg0),
                                (set_party_battle_mode),
#                                (set_jump_mission,"mt_charge_with_allies"),
                                (set_jump_mission,"mt_lead_charge"),
                                (jump_to_scene,"scn_random_scene"),
                                (change_screen_mission),
                                ]),

      ("join_order_attack",[
#          (gt, "$encountered_party_hostile", 0),
          (call_script, "script_party_count_fit_regulars", "p_main_party"),(ge, reg(0), 2),
          ],
           "Order your troops to join your allies while you stay back.",[(jump_to_menu,"mnu_join_order_attack"),
                                                            ]),
      
#      ("join_attack",[],"Lead a charge against the enemies",[(set_jump_mission,"mt_charge_with_allies"),
#                                                             (jump_to_scene,"scn_random_scene"),
#                                                             (change_screen_mission,0)]),
      ("join_leave",[],"Leave.",[(leave_encounter),(change_screen_return)]),
    ]
  ),


  (
    "join_order_attack",0,
    "{s4} Your casualties: {s1}. Allies' casualties: {s2}. Enemy casualties: {s3}.",
    "none",
    [
                                    (call_script, "script_party_calculate_regular_strength", "p_main_party"),
                                    (assign, ":player_party_strength", reg0),
                                    (val_div, ":player_party_strength", 5),
                                    (call_script, "script_party_calculate_regular_strength", "p_collective_ally"),
                                    (assign, ":ally_party_strength", reg0),
                                    (val_div, ":ally_party_strength", 5),
                                    (call_script, "script_party_calculate_regular_strength", "p_collective_enemy"),
                                    (assign, ":enemy_party_strength", reg0),
                                    (val_div, ":enemy_party_strength", 5),



                                    (store_add, ":friend_party_strength", ":player_party_strength", ":ally_party_strength"),
                                    (assign, ":enemy_party_strength_for_p", ":enemy_party_strength"),
                                    (val_mul, ":enemy_party_strength_for_p", ":player_party_strength"),
                                    (val_div, ":enemy_party_strength_for_p", ":friend_party_strength"),

                                    (val_sub, ":enemy_party_strength", ":enemy_party_strength_for_p"),
                                    (inflict_casualties_to_party_group, "p_main_party", ":enemy_party_strength_for_p", "p_temp_casualties"),
                                    (call_script, "script_print_casualties_to_s0", "p_temp_casualties"),
                                    (str_store_string_reg, s1, s0),
                                    
                                    (inflict_casualties_to_party_group, "$g_enemy_party", ":friend_party_strength", "p_temp_casualties"),
                                    (call_script, "script_print_casualties_to_s0", "p_temp_casualties"),
                                    (str_store_string_reg, s3, s0),
                                    (party_collect_attachments_to_party, "$g_ally_party", "p_collective_ally"),

                                    (inflict_casualties_to_party_group, "$g_ally_party", ":enemy_party_strength", "p_temp_casualties"),
                                    (call_script, "script_print_casualties_to_s0", "p_temp_casualties"),
                                    (str_store_string_reg, s2, s0),
                                    (party_collect_attachments_to_party, "$g_enemy_party", "p_collective_enemy"),

                                    (assign, "$cant_leave_encounter", 0),
                                    (assign, "$no_soldiers_left", 0),
                                    (try_begin),
                                      (call_script, "script_party_count_fit_regulars","p_main_party"),
                                      (le, reg(0), 0),
                                      (assign, "$no_soldiers_left", 1),
                                      (str_store_string, s4, "str_join_order_attack_failure"),
                                    (else_try),
                                      (call_script, "script_party_count_fit_regulars","p_collective_enemy"),
                                      (le, reg(0), 0),
                                      (assign, "$g_battle_result", 1),
                                      (assign, "$no_soldiers_left", 1),
                                      (str_store_string, s4, "str_join_order_attack_success"),
                                    (else_try),
                                      (str_store_string, s4, "str_join_order_attack_continue"),
                                    (try_end),
    ],
    [
      ("continue",[],"Continue...",[
          (jump_to_menu,"mnu_join_battle"),
          ]),
    ]
  ),

  
# Towns
  (
    "zendar",mnf_auto_enter,
    "You enter the town of Zendar.",
    "none",
    [(reset_price_rates,0),(set_price_rate_for_item,"itm_tools",70),(set_price_rate_for_item,"itm_salt",140)],
    [
      ("zendar_enter",[],"_",[(set_jump_mission,"mt_town_default"),(jump_to_scene,"scn_zendar_center"),(change_screen_mission)],"Door to the town centre."),
      ("zendar_tavern",[],"_",[(set_jump_mission,"mt_town_default"),
                                                   (jump_to_scene,"scn_the_happy_boar"),
                                                   (change_screen_mission)],"Door to the tavern."),
      ("zendar_merchant",[],"_",[(set_jump_mission,"mt_town_default"),
                                                   (jump_to_scene,"scn_zendar_merchant"),
                                                   (change_screen_mission)],"Door to the merchant."),
      ("zendar_arena",[],"_",[(set_jump_mission,"mt_town_default"),
                                                   (jump_to_scene,"scn_zendar_arena"),
                                                   (change_screen_mission)],"Door to the arena."),
#      ("zendar_leave",[],"Leave town.",[[leave_encounter],[change_screen_return]]),
      ("town_1_leave",[],"_",[(leave_encounter),(change_screen_return)]),
    ]
  ),
  (
    "salt_mine",mnf_auto_enter,
    "You enter the salt mine.",
    "none",
    [(reset_price_rates,0),(set_price_rate_for_item,"itm_salt",55)],
    [
      ("enter",[],"Enter.",[(set_jump_mission,"mt_visit_town_horseback"),(jump_to_scene,"scn_salt_mine"),(change_screen_mission)]),
      ("leave",[],"Leave.",[(leave_encounter),(change_screen_return)]),
    ]
  ),
  (
    "four_ways_inn",mnf_auto_enter,
    "You enter the four ways inn.",
    "none",
    [(reset_price_rates,0)],
    [

#      ("enter",[],"Enter.",[[set_jump_mission,"mt_town_default"],[jump_to_scene,"scn_conversation_scene"],[change_screen_mission]]),
      ("enter",[],"Enter.",[(set_jump_mission,"mt_visit_town_horseback"),(jump_to_scene,"scn_four_ways_inn"),(change_screen_mission)]),
      ("leave",[],"Leave.",[(leave_encounter),(change_screen_return)]),
    ]
  ),
  (
    "dhorak_keep",0,
#    "Dhorak Keep, the stronghold of the bandits stands overlooking the barren wilderness.",
    "You enter the Dhorak Keep",
    "none",
    [(reset_price_rates,0)],
    [
      ("enter",[],"Enter.",[(set_jump_mission,"mt_visit_town_horseback"),(jump_to_scene,"scn_dhorak_keep"),(change_screen_mission)]),
      ("leave",[],"Leave.",[(leave_encounter),(change_screen_return)]),
    ]
  ),
  (
    "training_ground",mnf_auto_enter,
    "You enter the training_ground",
    "none",
    [(reset_price_rates,0)],
    [
      ("enter_blank",[],"_",[
           (modify_visitors_at_site,"scn_training_ground"),(reset_visitors,0),
           (set_jump_mission,"mt_tutorial"),
           (jump_to_scene,"scn_training_ground"),(change_screen_mission)]),
      ("leave_blank",[],"_",[(leave_encounter),(change_screen_return)]),
    ]
  ),
  (
    "castle_outside",0,
    "You are outside {s1}. {s2} {s3}",
    "none",
    [
        (str_store_party_name, 1, "$g_encountered_party"),
        (assign,"$castle_undefended",0),
        (store_faction_of_party,":castle_faction",  "$g_encountered_party"),
        (store_relation, ":castle_relation", ":castle_faction", "p_main_party"),
        
        (try_begin),
          (eq, "$new_encounter", 1),
          (assign, "$new_encounter", 0),
          (assign, "$capture_screen_shown", 0),
          (assign, "$loot_screen_shown", 0),
          (assign, "$g_battle_result", 0),
          (assign, "$cant_leave_encounter", 0),
          (assign, "$cant_talk_to_enemy_party", 0),
          (assign, "$entry_to_town_forbidden",0),
          (assign, "$current_town","$g_encountered_party"),
          (assign,"$sneaked_into_town",0),
          (assign, "$ruler_meeting_denied",0),
          (assign, "$last_defeated_hero", 0),
          (assign, "$last_freed_hero", 0),
          (assign, "$town_entered", 0),
          (assign, "$waiting_for_arena_fight_result", 0),
          (call_script, "script_party_copy", "p_main_party_backup", "p_main_party"),
          (call_script, "script_party_copy", "p_encountered_party_backup", "$g_encountered_party"),
          (assign, "$encountered_party_hostile", 0),
          (try_begin),
            (lt, ":castle_relation", 0),
            (assign, "$encountered_party_hostile", 1),
            (assign,"$entry_to_town_forbidden",1),
          (try_end),

          (assign,"$cant_sneak_into_town",0),
          (try_begin),
            (eq,"$current_town","$last_sneak_attempt_town"),
            (store_current_hours,reg(2)),
            (val_sub,reg(2),"$last_sneak_attempt_time"),
            (lt,reg(2),12),
            (assign,"$cant_sneak_into_town",1),
          (try_end),

          (str_clear,3),
          (try_begin), 
            (eq,"$entry_to_town_forbidden",1),
            (try_begin),
              (eq,"$cant_sneak_into_town",1),
              (str_store_string,3,"str_sneaking_to_town_impossible"),
            (else_try),
              (str_store_string,3,"str_entrance_to_town_forbidden"),
            (try_end),
          (try_end),
        
        (else_try), #second or more turn
          (eq, "$g_leave_encounter",1),
          (change_screen_return),
        (try_end),

        (try_begin),
#          (eq, "$encountered_party_hostile", 1),
          (call_script, "script_party_count_fit_regulars","$g_encountered_party"),
          (assign, reg(3), reg(0)),
          (assign, reg(20),0),
          (try_begin),
            (eq, "$g_battle_result", 1),
   #         (eq, reg(3), 0), #battle won
            (assign, reg(20),1),
          (try_end),
          (this_or_next|eq, reg(20),1),
          (eq,"$g_enemy_surrenders",1),
          (try_begin),
            # Talk to enemy leaders
            (assign, ":done", 0),
            (party_get_num_companion_stacks, ":num_stacks","$g_encountered_party"),
            (try_for_range, ":stack_no", "$last_defeated_hero", ":num_stacks"),
              (party_stack_get_troop_id,   ":stack_troop","$g_encountered_party",":stack_no"),
              (troop_is_hero, ":stack_troop"),
              (store_add, "$last_defeated_hero", ":stack_no", 1),
              (assign, "$talk_context", tc_hero_defeated),
              (call_script, "script_setup_troop_meeting",":stack_troop"),
              (assign, ":done", 1),
            (try_end),
            (eq, ":done", 1),
          (else_try),
            # Talk to freed heroes
            (assign, ":done", 0),
            (party_get_num_prisoner_stacks, ":num_prisoner_stacks","$g_encountered_party"),
            (try_for_range, ":stack_no", "$last_freed_hero", ":num_prisoner_stacks"),
              (party_prisoner_stack_get_troop_id,   ":stack_troop","$g_encountered_party",":stack_no"),
              (troop_is_hero, ":stack_troop"),
              (store_add, "$last_freed_hero", ":stack_no", 1),
              (assign, "$talk_context", tc_hero_freed),
              (call_script, "script_setup_troop_meeting",":stack_troop"),
              (assign, ":done", 1),
            (try_end),
            (eq, ":done", 1),
          (else_try),
            (party_get_num_companions, reg(4), "$g_encountered_party"),
            (ge, reg(4), 1), #some wounded.
            (eq, "$capture_screen_shown", 0),
            (assign, "$capture_screen_shown", 1),
            (party_clear, "p_temp_party"),
            (assign, "$g_move_heroes", 0),
            (call_script, "script_party_prisoners_add_party_companions", "p_temp_party", "$g_encountered_party"),
            (change_screen_exchange_with_party, "p_temp_party"),
          (else_try),
            (eq, "$loot_screen_shown", 0),
            (assign, "$loot_screen_shown", 1),
            (troop_clear_inventory, "trp_temp_troop"),
            (call_script, "script_party_calculate_loot", "p_encountered_party_backup", -1),
            (gt, reg0, 0),
            (change_screen_loot, "trp_temp_troop"),
          (else_try), #Castle Taken!
            (jump_to_menu, "mnu_castle_taken"),
          (try_end),
        (else_try),
#          (eq, "$encountered_party_hostile", 1),
          (call_script, "script_party_count_fit_regulars","p_main_party"),
          (assign, reg(3), reg(0)),
          (assign, reg(20),0),
          (try_begin),
            (eq, "$g_battle_result", -1),
            (eq, reg(3), 0), #all lost
            (assign, reg(20),1),
            (assign, "$g_move_heroes", 0),
            (call_script, "script_party_add_party_prisoners", "$g_encountered_party", "p_main_party"),
            (call_script, "script_party_prisoners_add_party_companions", "$g_encountered_party", "p_main_party"),
            (call_script, "script_party_remove_all_companions", "p_main_party"),
            (call_script, "script_party_remove_all_prisoners", "p_main_party"),
            (jump_to_menu, "mnu_captivity_start_castle_defeat"), 
          (else_try),
            (eq, "$g_battle_result", -1),
            (store_random_in_range, reg(22), 0, 10),
            (lt, reg(22), 5),
            (assign, reg(20),2),
            (jump_to_menu, "mnu_captivity_start_castle_defeat"), 
          (try_end),
        (try_end),

        (party_get_num_companions, reg(7),"$g_encountered_party"),
        (try_begin),
          (eq,reg(7),0),
          (assign,"$castle_undefended",1),
          (party_set_faction,"$g_encountered_party","fac_neutral"),
          (str_store_string, 2, "str_castle_is_abondened"),
        (else_try),
          (eq,":castle_faction","fac_player_faction"),
          (str_store_string, 2, "str_place_is_occupied_by_player"),
        (else_try),
          (lt, ":castle_relation", 0),
          (str_store_string, 2, "str_place_is_occupied_by_enemy"),
        (else_try),
          (str_store_string, 2, "str_place_is_occupied_by_friendly"),
        (try_end),
        (try_begin),
          (party_slot_eq,"$g_encountered_party", slot_party_type,spt_castle),
          (this_or_next|eq,":castle_faction","fac_player_faction"),
          (eq,"$castle_undefended",1),
          (jump_to_menu, "mnu_town"),
        (else_try),
          (party_slot_eq,"$g_encountered_party", slot_party_type,spt_town),
          (gt, ":castle_relation", -1),
          (jump_to_menu, "mnu_town"),
        (try_end),
        ],
    [
      ("town_sneak",[(party_slot_eq,"$g_encountered_party", slot_party_type,spt_town),
                     (eq,"$entry_to_town_forbidden",1),
                     (eq,"$cant_sneak_into_town",0)],
       "Disguise yourself and try to sneak through the town gates.",[
          (store_random,reg(9),100),
          (try_begin),
            (lt, reg(9), 50),
            (jump_to_menu,"mnu_sneak_into_town_caught"),
          (else_try),
            (assign, "$sneaked_into_town",1),
            (jump_to_menu,"mnu_sneak_into_town_suceeded"),
          (try_end)]),
      ("castle_attack_walls",
       [
           (party_slot_eq,"$g_encountered_party", slot_party_type,spt_castle),
           (store_faction_of_party,reg(3),  "$g_encountered_party"),
           (store_relation, reg(5), reg(3), "fac_player_faction"),
           (lt, reg(5), 0),
           (lt, "$g_encountered_party_2", 1),
           (call_script, "script_party_count_fit_regulars","p_main_party"),
           (gt, reg(0), 0),
        ],
         "Attack the walls.",
         [
             (jump_to_menu, "mnu_castle_attack_walls_begin"),
          ]),
      ("castle_leave",[],"Leave...",[(change_screen_return,0)]),
    ]
  ),

   (
    "castle_attack_walls_begin",0,
    "Ladders need to be placed against the walls before the attack can start...",
    "none",
    [],
    [
      ("order_place_ladders",[],"Send some of your soldiers to place ladders on the walls.", [
                                    (jump_to_menu,"mnu_castle_attack_walls_2"),
                                    ]),
      ("order_retreat",[],"Call your soldiers back.",[(jump_to_menu,"mnu_castle_outside")]),
    ]
  ),
   (
    "castle_attack_walls_2",0,
    "A squad of your troops move in to place ladders on the walls, but they are met by a deadly barrage of arrows and stones. {s2} Your casualties were:{s1}.",
    "none",
    [
        (call_script, "script_party_calculate_regular_strength", "$g_encountered_party"),
        (assign, ":enemy_party_strength", reg0),
        (val_div, ":enemy_party_strength", 3),

        (call_script,"script_inflict_casualties_to_party", "p_main_party", ":enemy_party_strength"),
        (call_script, "script_print_casualties_to_s0", "p_temp_casualties"),
        (str_store_string_reg, s1, s0),

        (assign, "$no_soldiers_left", 0),
        (try_begin),
          (call_script, "script_party_count_fit_regulars","p_main_party"),
          (le, reg(0), 0),
          (assign, "$no_soldiers_left", 1),
          (str_store_string, s2, "str_place_ladders_failure"),
        (else_try),
          (str_store_string, s2, "str_place_ladders_success"),
        (try_end),
    ],
    [
      ("castle_lead_attack",[(eq, "$no_soldiers_left", 0)],"Lead the attack personally.", [
           (party_get_slot, ":battle_scene", "$g_encountered_party", slot_castle_exterior),
           (set_party_battle_mode),
           (set_jump_mission,"mt_castle_attack_walls"),
           (jump_to_scene,":battle_scene"),
           (jump_to_menu,"mnu_castle_outside"),
           (change_screen_mission),
       ]),
      ("continue_attacking_stay_back",[(eq, "$no_soldiers_left", 0)],"Order your soldiers to keep attacking as you stay back...", [
                                    (jump_to_menu,"mnu_castle_attack_walls_3"),
                                    ]),
      ("call_soldiers_back",[(eq, "$no_soldiers_left", 0)],"Call your soldiers back.",[(jump_to_menu,"mnu_castle_outside")]),
      ("continue",[(eq, "$no_soldiers_left", 1)],"Continue...",[(jump_to_menu,"mnu_castle_outside")]),
    ]
  ),
   (
    "castle_attack_walls_3",0,
    "{s3} Your casualties were:{s1}. Enemy casualties were: {s2}",
    "none",
    [
        (call_script, "script_party_calculate_regular_strength", "p_main_party"),
        (assign, ":player_party_strength", reg0),
        (val_div, ":player_party_strength", 10),

        (call_script, "script_party_calculate_regular_strength", "$g_encountered_party"),
        (assign, ":enemy_party_strength", reg0),
        (val_div, ":enemy_party_strength", 4),

        (call_script,"script_inflict_casualties_to_party", "p_main_party", ":enemy_party_strength"),
        (call_script, "script_print_casualties_to_s0", "p_temp_casualties"),
        (str_store_string_reg, s1, s0),

        (call_script,"script_inflict_casualties_to_party", "$g_encountered_party", ":player_party_strength"),
        (call_script, "script_print_casualties_to_s0", "p_temp_casualties"),
        (str_store_string_reg, s2, s0),

        (assign, "$no_soldiers_left", 0),
        (try_begin),
          (call_script, "script_party_count_fit_regulars","p_main_party"),
          (le, reg(0), 0),
          (assign, "$no_soldiers_left", 1),
          (str_store_string, s3, "str_attack_walls_failure"),
        (else_try),
          (call_script, "script_party_count_fit_regulars","$g_encountered_party"),
          (le, reg(0), 0),
          (assign, "$no_soldiers_left", 1),
          (assign, "$g_battle_result", 1),
          (str_store_string, s3, "str_attack_walls_success"),
        (else_try),
          (str_store_string, s3, "str_attack_walls_continue"),
        (try_end),
     ],
    [
      ("lead_next_wave",[(eq, "$no_soldiers_left", 0)],"Lead the next wave of attack personally.", [
           (party_get_slot, ":battle_scene", "$g_encountered_party", slot_castle_exterior),
           (set_party_battle_mode),
           (set_jump_mission,"mt_castle_attack_walls"),
           (jump_to_scene,":battle_scene"),
           (jump_to_menu,"mnu_castle_outside"),
           (change_screen_mission),
       ]),
      ("continue_attacking",[(eq, "$no_soldiers_left", 0)],"Order your soldiers to keep attacking...", [
                                    (jump_to_menu,"mnu_castle_attack_walls_3"),
                                    ]),
      ("call_soldiers_back",[(eq, "$no_soldiers_left", 0)],"Call your soldiers back.",[(jump_to_menu,"mnu_castle_outside")]),
      ("continue",[(eq, "$no_soldiers_left", 1)],"Continue...",[(jump_to_menu,"mnu_castle_outside")]),
    ]
  ),
  (
    "castle_taken",0,
    "The rest of the castle garrison recognizes that their situation is hopeless and surrenders. {s1} is at your mercy now.",
    "none",
    [
        (party_clear, "$g_encountered_party"),
    ],
    [
      ("continue",[],"Continue...",
       [
            (jump_to_menu, "mnu_town"),
        ]),
    ],
  ),

  (
    "town",0,
    "{s1}{s2}. {s3}",
    "none",
    [
#        (display_message, 0),
        (store_encountered_party,"$current_town"),
        (reset_price_rates,0),
        (party_get_slot, reg(10), "$current_town", slot_town_export_good),
        (party_get_slot, reg(11), "$current_town", slot_town_export_rate),
        (set_price_rate_for_item,reg(10),reg(11)),
        (party_get_slot, reg(10), "$current_town", slot_town_export_good_2),
        (party_get_slot, reg(11), "$current_town", slot_town_export_rate_2),
        (set_price_rate_for_item,reg(10),reg(11)),
        (party_get_slot, reg(10), "$current_town", slot_town_import_good),
        (party_get_slot, reg(11), "$current_town", slot_town_import_rate),
        (set_price_rate_for_item,reg(10),reg(11)),
        (party_get_slot, reg(10), "$current_town", slot_town_import_good_2),
        (party_get_slot, reg(11), "$current_town", slot_town_import_rate_2),
        (set_price_rate_for_item,reg(10),reg(11)),
        
        (assign,"$all_doors_locked",0),

        (try_begin),
          (eq, "$g_town_visit_after_rest", 1),
          (assign, "$g_town_visit_after_rest", 0),
          (assign, "$town_entered", 1),
        (try_end),


        (str_store_party_name,2,"$current_town"),
        (try_begin), 
          (party_slot_eq,"$current_town",slot_party_type, spt_castle),
          (str_store_string,1,"str_you_are_in_castle"),
        (else_try),
          (eq,"$entry_to_town_forbidden",0),
          (str_store_string,1,"str_you_are_in_town"),
        (else_try),
          (str_store_string,1,"str_you_sneaked_into_town"),
        (try_end),

        #forbidden to enter?
        (try_begin), 
          (store_time_of_day,reg(12)),
          (ge,reg(12),5),
          (lt,reg(12),21),
          (assign,"$town_nighttime",0),
          (str_store_string,3,"str_empty_string"),
        (else_try),
          (assign,"$town_nighttime",1),
          (str_store_string,3,"str_town_nighttime"),
        (try_end),


        (assign,"$castle_undefended",0),
        (party_get_num_companions, ":castle_garrison_size","$g_encountered_party"),
        (try_begin),
          (eq,":castle_garrison_size",0),
          (assign,"$castle_undefended",1),
          (party_set_faction,"$g_encountered_party","fac_neutral"),
          (try_begin),
            (party_slot_eq,"$current_town",slot_party_type, spt_castle),
            (str_store_string, 3, "str_castle_is_abondened"),
          (else_try),
            (str_store_string, 3, "str_town_is_abondened"),
          (try_end),
        (else_try),
          (store_faction_of_party,reg(3),  "$g_encountered_party"),
          (eq,reg(3),"fac_player_faction"),
          (str_store_string, 3, "str_place_is_occupied_by_player"),
        (else_try),
          (store_faction_of_party,":encountered_faction",  "$g_encountered_party"),
          (store_relation, ":relation", ":encountered_faction", "fac_player_faction"),
          (try_begin),
            (lt, ":relation", 0),
            (str_store_string, 3, "str_place_is_occupied_by_enemy"),
          (else_try),
            (str_store_string, 3, "str_place_is_occupied_by_friendly"),
          (try_end),
        (try_end),
        
        ],
    [
      ("request_meeting",[(eq,1,0),(eq,"$entry_to_town_forbidden",1),(eq,"$ruler_meeting_denied",0),(eq,"$sneaked_into_town",0)],
       "Request a meeting with the lord.",
       [
           (store_encountered_party,"$last_sneak_attempt_town"),
           (store_current_hours,"$last_sneak_attempt_time"),
           (jump_to_menu,"mnu_lord_meeting_accepted"),
        ]),
      ("disguise_and_sneak",[(eq,1,0),(eq,"$entry_to_town_forbidden",1),(eq,"$sneaked_into_town",0),(eq,"$cant_sneak_into_town",0)],
       "Disguise yourself and try to sneak through the town gates.",[
          (store_random,reg(9),100),
          (try_begin),
            (lt, reg(9), 50),
            (jump_to_menu,"mnu_sneak_into_town_caught"),
          (else_try),
            (assign, "$sneaked_into_town",1),
            (jump_to_menu,"mnu_sneak_into_town_suceeded"),
          (try_end),
          ]),
      ("town_castle",[(party_slot_eq,"$current_town",slot_party_type, spt_town),(eq,"$entry_to_town_forbidden",0)],"Go to the castle.",
       [
           (try_begin),
             (eq,"$all_doors_locked",1),
             (display_message,"str_door_locked",0xFFFFAAAA),
           (else_try),
             (assign, "$town_entered", 1),
             (set_jump_mission,"mt_visit_town_castle"),
             (party_get_slot, ":castle_scene", "$current_town", slot_town_castle),
             (jump_to_scene,":castle_scene"),
             (change_screen_mission),
           (try_end),
        ], "Door to the castle."),
      ("town_center",[
          (party_slot_eq,"$current_town",slot_party_type, spt_town),
          (this_or_next|eq,"$entry_to_town_forbidden",0),
          (eq, "$sneaked_into_town",1)]
       ,"Go to the town square.",
       [
           (try_begin),
             (eq,"$town_nighttime",0),
             (party_get_slot, reg(11), "$current_town", slot_town_center),
           (else_try),
             (party_get_slot, reg(11), "$current_town", slot_town_center),
           (try_end),

           (modify_visitors_at_site,reg(11)),(reset_visitors),
           (store_faction_of_party, reg(20),"$current_town"),
           (try_begin),
             (eq,reg(20),"fac_swadians"),
             (assign,reg(0),"trp_swadian_infantry"),
             (assign,reg(1),"trp_swadian_infantry"),
             (assign,reg(2),"trp_swadian_crossbowman"),
             (assign,reg(3),"trp_swadian_footman"),
           (else_try),
             (assign,reg(0),"trp_vaegir_infantry"),
             (assign,reg(1),"trp_vaegir_infantry"),
             (assign,reg(2),"trp_vaegir_archer"),
             (assign,reg(3),"trp_vaegir_footman"),
           (try_end),
           (shuffle_range,0,4),
           (set_visitor,25,reg(0)),
           (set_visitor,26,reg(1)),
           (set_visitor,27,reg(2)),
           (set_visitor,28,reg(3)),

           (set_jump_mission,"mt_town_default"),
           (try_begin),
             (eq, "$sneaked_into_town",1),
             (set_jump_mission,"mt_town_disguised"),
           (else_try),
             (eq, "$town_entered", 0),
             (assign, "$town_entered", 1),
             (set_jump_mission,"mt_visit_town_horseback"),
             (set_jump_entry,1),
           (try_end),
           (jump_to_scene,reg(11)),
           (change_screen_mission),
        ],"Door to the town centre."),

      ("town_tavern",[
          (party_slot_eq,"$current_town",slot_party_type, spt_town),
          (this_or_next|eq,"$entry_to_town_forbidden",0),
          (eq, "$sneaked_into_town",1),
          (party_get_slot, reg(1), "$current_town", slot_town_tavern),
          (scene_get_slot, reg(2), reg(1), slot_scene_visited),
          (ge, reg(2), 1), #check if scene has been visited before to allow entry from menu. Otherwise scene will only be accessible from the town center.
          ]
       ,"Visit the tavern.",
       [
           (try_begin),
             (eq,"$all_doors_locked",1),
             (display_message,"str_door_locked",0xFFFFAAAA),
           (else_try),
             (try_begin),
               (eq, "$sneaked_into_town",1),
               (assign,reg(10),"mt_town_disguised"),
             (else_try),
               (assign,reg(10),"mt_town_default"),
             (try_end),
             (assign, "$town_entered", 1),
             (set_jump_mission,reg(10)),
             (party_get_slot, reg(11), "$current_town", slot_town_tavern),
             (jump_to_scene,reg(11)),
             (scene_set_slot, reg(11), slot_scene_visited, 1),

            (modify_visitors_at_site,reg(11)),(reset_visitors),
            (assign, reg(14), 17),
            (try_for_parties, reg(16)),
               (party_slot_eq, reg(16), slot_party_type, spt_caravan),
               (party_is_in_town, reg(16), "$current_town"),
               (lt, reg(14), 32), # use up entry point 32
               (party_stack_get_troop_id, reg(18), reg(16), 0),
               (set_visitor, reg(14), reg(18)),
             (try_end),
             (change_screen_mission),
           (try_end,0),
        ],"Door to the tavern."),
      
#      ("town_smithy",[
#          (eq,"$entry_to_town_forbidden",0),
#          (eq,"$town_nighttime",0),
#          ],
#       "Visit the smithy.",
#       [
#           (set_jump_mission,"mt_town_default"),
#           (jump_to_scene,"$pout_scn_smithy"),
#           (change_screen_mission,0),
#        ]),
      
      ("town_merchant",
       [(party_slot_eq,"$current_town",slot_party_type, spt_town),
           (eq,"$town_nighttime",0),
           (this_or_next|eq,"$entry_to_town_forbidden",0),
           (eq, "$sneaked_into_town",1),
           (party_get_slot, reg(1), "$current_town", slot_town_store),
           (scene_get_slot, reg(2), reg(1), slot_scene_visited),
           (ge, reg(2), 1), #check if scene has been visited before to allow entry from menu. Otherwise scene will only be accessible from the town center.
           ],
       "Speak with the Merchant.",
       [
           
           (try_begin),
             (this_or_next|eq,"$all_doors_locked",1),
             (eq,"$town_nighttime",1),
             (display_message,"str_door_locked",0xFFFFAAAA),
           (else_try),
             (try_begin),
               (eq, "$sneaked_into_town",1),
               (assign,reg(10),"mt_town_disguised"),
             (else_try),
               (assign,reg(10),"mt_town_default"),
             (try_end),
             (assign, "$town_entered", 1),
             (set_jump_mission,reg(10)),
             (party_get_slot, reg(11), "$current_town", slot_town_store),
             (jump_to_scene,reg(11)),
             (scene_set_slot, reg(11), slot_scene_visited, 1),
             (change_screen_mission),
           (try_end),
        ],"Door to the shop."),
      ("town_arena",
       [(party_slot_eq,"$current_town",slot_party_type, spt_town),
           ],
       "Enter the arena.",
       [
           
           (try_begin),
             (this_or_next|eq,"$all_doors_locked",1),
             (eq,"$town_nighttime",1),
             (display_message,"str_door_locked",0xFFFFAAAA),
           (else_try),
             (try_begin),
               (eq, "$sneaked_into_town",1),
               (assign,reg(10),"mt_visit_town_arena_disguised"),
             (else_try),
               (assign,reg(10),"mt_visit_town_arena"),
             (try_end),
             (assign, "$town_entered", 1),
             (set_jump_mission,reg(10)),
             (party_get_slot, reg(11), "$current_town", slot_town_arena),
             (set_jump_entry, 50),
             (jump_to_scene,reg(11)),
             (scene_set_slot, reg(11), slot_scene_visited, 1),
             (change_screen_mission),
           (try_end),
        ],"Door to the arena."),
      ("castle_inspect", [(party_slot_eq,"$current_town",slot_party_type, spt_castle),(store_faction_of_party,reg(3),  "$current_town"),(this_or_next|eq,reg(3),"fac_player_faction"),(eq,"$castle_undefended",1)],
       "Inspect the castle.",
       [
           (party_get_slot, reg(10), "$current_town", slot_castle_exterior),
           (set_jump_mission,"mt_castle_visit"),
           (jump_to_scene,reg(10)),
           (change_screen_mission),
        ]),
      ("castle_station_troops",
       [
           (party_slot_eq,"$current_town",slot_party_type, spt_castle),
           (store_faction_of_party,reg(3),  "$current_town"),
           (this_or_next|eq,reg(3),"fac_player_faction"),
           (eq,"$castle_undefended",1),
        ],
         "Station some troops here",
         [
           (party_set_faction,"$current_town","fac_player_faction"),
           (change_screen_exchange_members,0),
          ]),

      ("castle_rest_until_morning",
       [
           (party_slot_eq,"$current_town",slot_party_type, spt_castle),
           (store_faction_of_party,reg(3),  "$current_town"),
           (this_or_next|eq,reg(3),"fac_player_faction"),
           (eq,"$castle_undefended",1),
           (store_time_of_day,reg(1)),(neg|is_between,reg(1), 5, 18),
        ],
         "Rest here until morning",
         [
           (party_set_faction,"$current_town","fac_player_faction"),
           (store_time_of_day,reg(1)),
           (assign, reg(2), 30),
           (val_sub,reg(2),reg(1)),
           (val_mod,reg(2),24),
           (assign,"$auto_enter_town","$current_town"),
#           (assign,"$auto_enter_town_menu","mnu_castle_within_walls"),
           (rest_for_hours, reg(2)),
           (change_screen_return),

           (store_current_hours, "$defended_until_time"),
           (val_add,"$defended_until_time",reg(2)),
           (assign, "$defended_location", "$current_town"),
          ]),
      
      ("castle_rest_until_evening",
       [
           (party_slot_eq,"$current_town",slot_party_type, spt_castle),
           (store_faction_of_party,reg(3),  "$current_town"),
           (this_or_next|eq,reg(3),"fac_player_faction"),
           (eq,"$castle_undefended",1),
           (store_time_of_day,reg(1)), (is_between,reg(1), 5, 18),
        ],
         "Rest here until evening",
         [
           (party_set_faction,"$current_town","fac_player_faction"),
           (store_time_of_day,reg(1)),
           (assign, reg(2), 20),
           (val_sub,reg(2),reg(1)),
           (assign,"$auto_enter_town","$current_town"),
#           (assign,"$auto_enter_town_menu","mnu_castle_within_walls"),
           (rest_for_hours, reg(2)),
           (change_screen_return),

           (store_current_hours, "$defended_until_time"),
           (val_add,"$defended_until_time",reg(2)),
           (assign, "$defended_position", "$current_town"),
          ]),
      ("town_leave",[],"Leave...",[(change_screen_return,0)]),
    ]
  ),
  
  (
    "lord_meeting_accepted",0,
    "The guards tell you that the lord has granted you a meeting.",
    "none",
    [],
    [
      ("lord_meeting_go",[],"Go to meet the lord.",[
          (jump_to_menu,"mnu_town"),
          (party_get_slot, reg(1), "$current_town", slot_town_castle),
          (jump_to_scene,reg(1)),
          (set_jump_mission,"mt_visit_town_castle"),
          (change_screen_mission),
          ]),
      ("lord_meeting_dont_go",[],"Turn back and leave.",[(change_screen_return,0)]),
    ]
  ),
  (
    "sneak_into_town_suceeded",0,
    "Disguised in the garments of a poor pilgrim, you fool the guards and make your way into the town.",
    "none",
    [],
    [
      ("continue",[],"Continue...",
       [
           (assign, "$sneaked_into_town",1),
           (jump_to_menu,"mnu_town"),
        ]),
    ]
  ),
  (
    "sneak_into_town_caught",0,
    "As you try to sneak in, one of the guards recognizes you and gives a warning!\
 You must flee back through the gates before all the guards in the town rush against you.",
    "none",
    [
       (assign,"$auto_menu","mnu_captivity_start_castle_surrender"),
    ],
    [
      ("sneak_caught_fight",[],"Try to fight your way out!",
       [
           (assign,"$all_doors_locked",1),
           (party_get_slot, ":sneak_scene", "$current_town",slot_town_center), # slot_town_gate),
           (modify_visitors_at_site,":sneak_scene"),(reset_visitors),
           (set_visitor,0,"trp_player"),
           (store_faction_of_party, ":town_faction","$current_town"),
           (try_begin),
             (eq, ":town_faction", "fac_vaegirs"),
             (assign,reg(0),"trp_vaegir_skirmisher"),
             (assign,reg(1),"trp_vaegir_archer"),
             (assign,reg(2),"trp_vaegir_infantry"),
             (assign,reg(3),"trp_vaegir_archer"),
           (else_try),
             (assign,reg(0),"trp_swadian_skirmisher"),
             (assign,reg(1),"trp_swadian_crossbowman"),
             (assign,reg(2),"trp_swadian_infantry"),
             (assign,reg(3),"trp_swadian_crossbowman"),
           (try_end),
           (assign,reg(4),-1),
           (shuffle_range,0,5),
           (set_visitor,1,reg(0)),
           (set_visitor,2,reg(1)),
           (set_visitor,3,reg(2)),
           (set_visitor,4,reg(3)),
           (set_jump_mission,"mt_sneak_caught_fight"),
 #          (jump_to_menu,"mnu_captivity_start_castle_defeat"),
           (set_passage_menu,"mnu_town"),
           (jump_to_scene,":sneak_scene"),
           (change_screen_mission),
        ]),
      ("sneak_caught_surrender",[],"Surrender.",
       [
           (jump_to_menu,"mnu_captivity_start_castle_surrender"),
        ]),
    ]
  ),
  (
    "sneak_into_town_caught_ran_away",0,
    "You leave the town behind and run away to the safety of the hills.",
    "none",
    [],
    [
      ("continue",[],"Continue...",
       [
           (assign,"$auto_menu",-1),
           (store_encountered_party,"$last_sneak_attempt_town"),
           (store_current_hours,"$last_sneak_attempt_time"),
           (change_screen_return),
        ]),
    ]
  ),




#####################################################################
## Captivity....
#####################################################################
#####################################################################
#####################################################################
#####################################################################
  
  (
    "captivity_start_wilderness_surrender",0,
    "Stub",
    "none",
    [
       (assign,"$auto_menu",-1), #We need this since we may come here by something other than auto_menu
       (assign, "$capturer_party", "$g_encountered_party"),
       (jump_to_menu, "mnu_captivity_wilderness_taken_prisoner"),
      ],
    []
  ),
  (
    "captivity_start_wilderness_defeat",0,
    "Your enemies take you as prisoner.",
    "none",
    [
       (assign,"$auto_menu",-1),
       (assign, "$capturer_party", "$g_encountered_party"),
       (jump_to_menu, "mnu_captivity_wilderness_taken_prisoner"),
    ],
    []
  ),
  (
    "captivity_start_castle_surrender",0,
    "Stub",
    "none",
    [
       (assign,"$auto_menu",-1),
       (assign, "$capturer_party", "$g_encountered_party"),
       (jump_to_menu, "mnu_captivity_castle_taken_prisoner"),
      ],
    []
  ),
  (
    "captivity_start_castle_defeat",0,
    "Stub",
    "none",
    [
       (assign,"$auto_menu",-1),
       (assign, "$capturer_party", "$g_encountered_party"),
       (jump_to_menu, "mnu_captivity_castle_taken_prisoner"),
      ],
    []
  ),
  (
    "captivity_wilderness_taken_prisoner",0,
    "Your enemies take you as prisoner.",
    "none",
    [],
    [
      ("continue",[],"Continue...",
       [
           (store_random_in_range, reg(8), 12, 19),
           (rest_for_hours, reg(8)),
           (assign,"$auto_menu","mnu_captivity_wilderness_check"),
           (change_screen_return),
        ]),
    ]
  ),
  (
    "captivity_wilderness_check",0,
    "stub",
    "none",
    [(jump_to_menu,"mnu_captivity_end_wilderness_escape")],
    []
  ),
  (
    "captivity_end_wilderness_escape",0,
    "After painful days of being dragged about as a prisoner, you find a chance and escape from your captors!",
    "none",
    [],
    [
      ("continue",[],"Continue...",
       [
           (assign, "$capturer_party", "$g_encountered_party"),
           (party_relocate_near_party, "p_main_party", "$capturer_party", 2),
           (change_screen_return),
        ]),
    ]
  ),
  (
    "captivity_castle_taken_prisoner",0,
    "You are quickly surrounded by guards who take away your weapons. With curses and insults, they throw you into the dungeon where you will spend the miserable days of your captivity.",
    "none",
    [],
    [
      ("continue",[],"Continue...",
       [
           (store_random_in_range, reg(8), 16, 22),
           (rest_for_hours, reg(8)),
           (assign,"$auto_menu", "mnu_captivity_castle_check"),
           (change_screen_return)
        ]),
    ]
  ),
  (
    "captivity_castle_check",0,
    "stub",
    "none",
    [
        (store_random_in_range, reg(7), 0, 10),
        (try_begin),
          (lt, reg(7), 4),
          (store_random_in_range, "$player_ransom_amount", 3, 6),
          (val_mul, "$player_ransom_amount", 100),
          (store_troop_gold, reg(3), "trp_player"),
          (gt, reg(3), "$player_ransom_amount"),
          (jump_to_menu,"mnu_captivity_end_propose_ransom"),
        (else_try),
          (lt, reg(7), 7),
          (jump_to_menu,"mnu_captivity_end_exchanged_with_prisoner"),
        (else_try),
          (jump_to_menu,"mnu_captivity_castle_remain"),
        (try_end),
    ],
    []
  ),
  (
    "captivity_end_exchanged_with_prisoner",0,
    "After days of imprisonment, you are finally set free when your captors exchange you with a prisoner of their own.",
    "none",
    [],
    [
      ("continue",[],"Continue...",
       [
           (party_relocate_near_party, "p_main_party", "$capturer_party", 1),
           (change_screen_return),
        ]),
    ]
  ),
  (
    "captivity_end_propose_ransom",0,
    "After some time your captors come to you with an offer: In return for {reg5} denars of your hidden wealth, they propose to set you free. You decide to...",
    "none",
    [
        (store_character_level, ":player_level", "trp_player"),
        (store_mul, "$player_ransom_amount", ":player_level", 50),
        (val_add, "$player_ransom_amount", 100),
        (assign, reg5, "$player_ransom_amount"),
    ],
    [
      ("captivity_end_ransom_accept",[(store_troop_gold,":player_gold", "trp_player"),(ge, ":player_gold","$player_ransom_amount")],"Accept the offer.",
       [
           (troop_remove_gold, "trp_player", "$player_ransom_amount"), 
           (party_relocate_near_party, "p_main_party", "$capturer_party", 1),
           (rest_for_hours, 0),
           (change_screen_return),
        ]),
      ("captivity_end_ransom_deny",[],"Wait for a better chance.",
       [
           (store_random_in_range, reg(8), 16, 22),
           (rest_for_hours, reg(8)),
           (assign,"$auto_menu", "mnu_captivity_castle_check"),
           (change_screen_return),
        ]),
    ]
  ),
  (
    "captivity_castle_remain",0,
    "Days pass in the darkness of your cell where you have to endure the hardships of captivity and the scornful jokes of the guards...",
    "none",
    [
        (store_random_in_range, reg(8), 16, 22),
        (rest_for_hours, reg(8)),
        (assign,"$auto_menu", "mnu_captivity_castle_check"),
    ],
    [
      ("continue",[],"Continue...",
       [
           (change_screen_return),
        ]),
    ]
  ),
  
 ]

